<?php
declare (strict_types=1);

namespace app\admin\controller;


use app\common\facade\Builder;
use app\common\model\Level;
use app\common\model\ProductCate;
use app\common\model\QuickCities;

use think\App;
use think\facade\View;
use think\response\Json;

class Product extends Base
{

    //主键，默认为id
    protected string $pk = 'id';

    //主模型
    protected string $model = '\app\common\model\Product';

    //验证器
    protected string $validate = 'Product';

    //关联模型
    protected array $hasOne = ['cate1','cate2'];

    public function index(): string
    {
        //构建table
        return Builder::Table()
            ->addColumn(['type' => 'checkbox', 'fixed' => 'left', 'field' => '', 'title' => '全选'])
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80])
            ->addColumn(['field' => 'title', 'title' => '商品名称', 'modify' => '2,input'])
            ->addColumn(['field' => 'price', 'title' => '价格', 'modify' => '2,input'])
//            ->addColumn(['field' => 'vip_price', 'title' => '会员价', 'modify' => '2,input'])
            ->addColumn(['field' => 'cate_1', 'title' => '商品分类', 'templet' => '<div>{{d.cate1.title}}</div>', 'modify' => '2,select,getCate'])
            ->addColumn(['field' => 'stock', 'title' => '库存', 'modify' => '2,input'])
            ->addColumn(['field' => 'images', 'title' => '缩略图', 'modify' => '2,img,1', 'templet' => '#imagesTpl'])
            ->addColumn(['field' => 'content', 'title' => '详情', 'modify' => '2,rich_text', 'templet' => '#editorTpl'])
            ->addColumn(['field' => 'sort', 'title' => '排序', 'modify' => '2,input', 'templet' => '#sort_sort', 'width' => 120])
            ->addColumn(['field' => 'create_time', 'title' => '创建时间'])
            ->addColumn(['field' => 'update_time', 'title' => '更新时间'])
            ->addColumn(['fixed' => 'right','field' => 'pick', 'title' => '推荐', 'templet' => '#pick', 'width' => 120])
            ->addColumn(['fixed' => 'right','field' => 'status', 'title' => '上架状态', 'templet' => '#status', 'width' => 120])
            ->addColumn(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 200])
            ->addTopButtons(['add' => ['size' => '1200,1200'], 'deleteAll'])
            ->addRightButtons(['edit' => ['size' => '1200,1200'], 'delete'])
            ->addSwitchTool([
                ['name' => 'status', 'pk' => 'id', 'text' => '开启|关闭'],
                ['name' => 'pick', 'pk' => 'id', 'text' => '开启|关闭'],
            ])
            ->addSortTool([['name' => 'sort', 'pk' => $this->pk, 'url' => url('update')->build()]])
            ->addSearch([
                ['title', '输入商品名称 ', 'input', '', '', '', 'like'],
                ['cate_1', '商品分类', 'select', ProductCate::where(['pid' => 0, 'is_del' => 0])->column('title', 'id')]
            ])
            ->addCensus('\app\common\model\Statistics', 'getProductCensus')
            ->fetch();
    }

    public static function getCate(): array
    {
        return ProductCate::where(['is_del' => 0])->column('title', 'id');
    }

    public static function getCity(): Json
    {
        $city_name = QuickCities::where(['is_del' => 0])->field(['id','city_name'])->select();
//        dump($city_name);die;
        return json($city_name);
    }

    public function update(): Json
    {
        try {
            $param = $this->request->post();


            return json($this->model::_update($param, $this->getLogsArguments($param)));
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }


    public function add($tpl = 'product/edit'): string
    {
        View::assign('form', ['title' => '商品图片', 'field' => 'images', 'value' => 5]);
        View::assign('level',  Level::where('is_del',0)->select());
        return parent::add($tpl); // TODO: Change the autogenerated stub
    }

    public function edit($id = '', $tpl = 'product/edit', $info = ''): string
    {
        View::assign('form', ['title' => '商品图片', 'field' => 'images', 'value' => 5]);
        View::assign('level',  Level::where('is_del',0)->select());
        return parent::edit($id, $tpl, $info); // TODO: Change the autogenerated stub
    }


    public function save($param = [], $model = false): Json
    {
        try {
            $param = $this->request->post();
//            dump($param);die;
            $cate = ProductCate::find($param['cate_1']);

            $param['icon'] = parse_url($cate->icon)['path'];

            if(isset($param['city_name'])&& $param['city_name'])
            {
               $quick = QuickCities::find($param['city_name']);
               $param['province'] = $quick['province'];
               $param['city'] = $quick['city'];
               $param['district'] = '';
            }
            if(isset($param['images']) && $param['images']){
                $param['icon'] = explode(',',$param['images'])[0];
            }
            return parent::save($param, $model); // TODO: Change the autogenerated stub
        } catch (\Exception $e) {
            dump($e->getMessage());die;

        }
    }


}
